<?php
    require_once "config.php";
    require_once "weixin_info.php";//微信授权获取用户头像等信息
    if(!empty($_GET['way'])){
        $way = $_GET['way'];
        if($way == 'delete'){
            $id = $_POST['id'];
            if(!empty($id)){
                $goods_sql = $mysql->query("select * from dyspw_goods where id=".$id);//è§†é¢‘
                $goods_info = $goods_sql->fetch_all(MYSQLI_ASSOC);
                if($goods_info){
                    $goods_del = $mysql->query("DELETE FROM dyspw_goods where id = ".$id);
                    if($goods_del){
                        $result['status'] = 'y';
                        $result['info'] = '商品删除成功，即将刷新';
                        exit(json_encode($result));
                    }else{
                        $result['status'] = 'n';
                        $result['info'] = '商品删除失败';
                        exit(json_encode($result));
                    }
                }else{
                    $result['status'] = 'n';
                    $result['info'] = '未查询到该商品信息';
                    exit(json_encode($result));
                }
            }else{
                $result['status'] = 'n';
                $result['info'] = '未获取到该商品id';
                exit(json_encode($result));
            }
        }
    }else{
        if($_SERVER['REQUEST_METHOD'] == 'POST'){

            $data = array(
                'title' => $_POST['title'],
                'goods_img' => $_POST['goods_img'],
                'money' => $_POST['money'],
                'group_money' => $_POST['group_money'],
                'details' => $_POST['details'],
                'info' => $_POST['info'],
                'introduce' => $_POST['introduce'],
                'u_id' => !empty($_SESSION['Member_info'][0]['shop_son_id']) ?  $_SESSION['Member_info'][0]['shop_son_id'] : $_SESSION['Member_info'][0]['id'],
                'info_img' => $_POST['info_img'],
                'time' => time(),
                'stock'=>$_POST['stock'],
                'rob'=>$_POST['rob'],
                'sales'=>$_POST['sales']
            );
            $way = $_POST['way'];
            if(empty($data['title']) || empty($data['goods_img']) || empty($data['money']) || empty($data['details'])){
                $result['status'] = 'n';
                $result['info'] = '所有信息为必填项，请重新输入';
                exit(json_encode($result));
            }

            $sql = $mysql->query("select * from dyspw_member where id=".$data['u_id']);//è§†é¢‘
            $member_info = $sql->fetch_all(MYSQLI_ASSOC);
            $data['u_name'] = $member_info[0]['username'];
            $data['u_avatar'] = $member_info[0]['avatar'];
            //商品名称唯一性
            if($way == 'edit'){
                $id = $_POST['id'];
                $goods_sql = $mysql->query("select * from dyspw_goods where title = '{$data['title']}' and id != {$id}");
                $goods = $goods_sql->fetch_all(MYSQLI_ASSOC);
            }else{
                $goods_sql = $mysql->query("select * from dyspw_goods where title = '{$data['title']}'");
                $goods = $goods_sql->fetch_all(MYSQLI_ASSOC);
            }
            // // print_r($goods);exit;
            // if(!empty($goods)){
            //     $result['status'] = 'n';
            //     $result['info'] = '此商品名称已存在，请重新输入';
            //     exit(json_encode($result));
            // }
            if($way == 'edit'){
                $goods_add = $mysql->query("UPDATE dyspw_goods SET title='{$data['title']}', img='{$data['goods_img']}', money='{$data['money']}',group_money='{$data['group_money']}',introduce='{$data['introduce']}',details='{$data['details']}',info='{$data['info']}',info_img='{$data['info_img']}',time='{$data['time']}',stock='{$data['stock']}',rob='{$data['rob']}',sales='{$data['sales']}' where id={$id}");
            }else{
                $goods_add = $mysql->query("insert into dyspw_goods (title,img,money,group_money,introduce,details,info, u_id,info_img,time,u_name,u_avatar, address_id,address_name,stock,rob,sales) values ('{$data['title']}','{$data['goods_img']}','{$data['money']}','{$data['group_money']}','{$data['introduce']}','{$data['details']}','{$data['info']}','{$data['u_id']}','{$data['info_img']}','{$data['time']}','{$data['u_name']}','{$data['u_avatar']}','{$_SESSION['Address']['Id']}','{$_SESSION['Address']['Name']}','{$data['stock']}','{$data['rob']}','{$data['sales']}')");
            }
            if($goods_add){
                $result['status'] = 'y';
                $result['info'] = '编辑商品成功';
                exit(json_encode($result));
            }else{
                $result['status'] = 'n';
                $result['info'] = '编辑商品失败，请重新提交';
                exit(json_encode($result));
            }
        }
    }

 ?>
